    .equ    ConsCtrl 2
    .equ    ConsData 3
    .equ    DiskCtrl 4
    .equ    DiskData 5
    .equ    READY 2
    .equ    SET0 0x10
    .equ    SET1 0x20
    .equ    SET2 0x40
    .equ    FAddr 0

.section .text at 0:
    .global _start
_start:
#print welcome word
    set     r0,@_string1
    set     r4,@_size1
    load    r1,1,r4+0
    call    @output
#load a sector to 0x7c00
#r0: file_addr
#r1: memory_addr
#r2: size
#r3: status
    set     r4,@_Addr
    load    r0,2,r4+0
    load    r1,2,r4+2
    load    r2,2,r4+0
    load    r31,2,r4+2
    set     r3,@SET0
    write   r3,1,@DiskCtrl                                          #Set Memory Address
    write   r1,8,@DiskData
    set     r3,@SET1
    write   r3,1,@DiskCtrl                                          #Set Disk Address
    write   r0,8,@DiskData
    set     r3,@SET2
    write   r3,1,@DiskCtrl
    set     r2,r2>>6
    write   r2,2,@DiskCtrl                                          #Set Disk read size
    set     r3,@READY
    write   r3,1,@DiskCtrl                                          #Run Disk read
    read    r3,1,@DiskCtrl                                          #Check Disk read status
    jmp     r3,ne,-2                                                #wait until disk read done
#print stage 1 word
    set     r0,@_string2
    set     r4,@_size1
    load    r1,1,r4+1
    call    @output
    set     r31,r31>>2
    jmp     r31+0

.section ints at 0x100:
    .global output
#handle output
#r0: beg_addr
#r1: size
output:
    load    r16,1,r0+0                                              #Load 1byte from addr
    set     r0,r0+1                                                 #Increase addr
    set     r1,r1-1                                                 #Decrease size
    write   r16,1,@ConsData                                         #Output 1byte data
    set     r17,@READY                                              #Set status to ready
    write   r17,1,@ConsCtrl                                         #Output control status
    jmp     r1,ge,@output                                           #Jump to begin if size > 0
    ret


.section .rodata at 0x180:
    .string _string1 0x180 "Hello, SandBox!\n"
    .string _string2 0x1a0 "Stage 1 loaded,turning to stage 2...\n"
    .2byte  _Addr 0x1d0 0x7c00,0x1000
    .byte   _size1 0x1e0 16,37